package com.qst.express.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qst.express.entity.Courier;
import com.qst.express.entity.Express;
import com.qst.express.entity.User;
import com.qst.express.entity.vo.UserVo;
import com.qst.express.mapper.UserMapper;
import com.qst.express.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mjh
 * @since 2022-09-25
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {


    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public IPage<User> pageTeacherCondition(Page<User> userPage, UserVo userVo) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();

        if (userVo != null ) {
            String uname = userVo.getUname();
            String uphone = userVo.getUphone();
            String begin = userVo.getBegin();
            String end = userVo.getEnd();
            if (!StringUtils.isEmpty(uname)) {
                queryWrapper.like("uname", uname);
            }
            if (!StringUtils.isEmpty(uphone)) {
                queryWrapper.like("uphone", uphone);
            }
            if (!org.springframework.util.StringUtils.isEmpty(begin)){
                queryWrapper.ge("createtime", begin);
            }
            if (!org.springframework.util.StringUtils.isEmpty(end)){
                queryWrapper.le("createtime", end);
            }
        }
        IPage<User> userIPage = this.baseMapper.selectPage(userPage, queryWrapper);
        return userIPage;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public boolean isFindByPhone(String userPhone) {
        User user= baseMapper.findByPhone(userPhone);
        if (user == null){
            return false;
        }else {
            return true;
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public boolean insert2(String userPhone) {
        User user = new User();
        user.setImg("https://edu-default-images.oss-cn-shenzhen.aliyuncs.com/2022/HearImg-default/three.jpg");
        user.setUphone(userPhone);
        user.setUname("新用户");
        user.setCreatetime(new Date());
        return baseMapper.insert(user)>0;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public void updateLoginTime(String userPhone) {
        baseMapper.updateLoginTime(userPhone);
    }
    //根据手机号查询用户信息
    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public User findUserByPhone(String userPhone) {
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.eq("uphone", userPhone);
        User user = baseMapper.selectOne(wrapper);
        return user;
    }
    //断用户是否认证
    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public boolean isIdentification(String phone) {
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.eq("uphone", phone);
        User user = baseMapper.selectOne(wrapper);
        if (user.getIdentification() == 0){
            return false;
        }else {
            return true;
        }

    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public boolean updateUser(User u) {
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.eq("uphone", u.getUphone());
        u.setIdentification(1);
        int update = baseMapper.update(u, wrapper);
        return update>0?true:false;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public User getImg(String userphone) {
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.eq("uphone", userphone);
        User user = baseMapper.selectOne(wrapper);
        return user;
    }
    @Transactional
    @Override
    public boolean updateUserName(String userPhone, String userName) {
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        User user = new User();
        user.setUname(userName);
        wrapper.eq("uphone", userPhone);
        int update = baseMapper.update(user, wrapper);
        return update>0?true:false;
    }
    @Transactional
    @Override
    public List<Map<String, Integer>> console() {
        List<Map<String, Integer>>  data = baseMapper.console();
        return  data;
    }
}
